using NUnit.Framework;

namespace ValidationFramework.Tests
{
    [TestFixture]
    public class RequiredStringRuleAttributeTest
    {
        [Test]
        public void CheckDefaultValues()
        {
            var ruleAttribute = new RequiredStringRuleAttribute();
            Assert.IsTrue(ruleAttribute.IgnoreCase);
            Assert.IsTrue(ruleAttribute.TrimWhiteSpace);

            var propertyRule = (RequiredStringRule)ruleAttribute.CreateRule(new MockPropertyDescriptor<string>("foo"));
			var parameterRule = (RequiredStringRule)ruleAttribute.CreateRule(new MockParameterDescriptor<string>("foo"));

            Assert.IsFalse(parameterRule.HasInitialValue);
            Assert.IsNull(parameterRule.IgnoreCase);
            Assert.IsTrue(parameterRule.TrimWhiteSpace);

            Assert.IsFalse(propertyRule.HasInitialValue);
			Assert.IsNull(propertyRule.IgnoreCase);
            Assert.IsTrue(propertyRule.TrimWhiteSpace);
        }

        [Test]
        public void CheckNonDefaultValuesIgnoreSpaces()
        {
            var ruleAttribute = new RequiredStringRuleAttribute {TrimWhiteSpace = true};
            Assert.IsNull(ruleAttribute.InitialValue);

            var propertyRule = (RequiredStringRule)ruleAttribute.CreateRule(new MockPropertyDescriptor<string>("foo"));
            var parameterRule = (RequiredStringRule)ruleAttribute.CreateRule(new MockParameterDescriptor<string>("foo"));

            Assert.IsFalse(parameterRule.HasInitialValue);
            Assert.IsTrue(parameterRule.TrimWhiteSpace);

            Assert.IsFalse(propertyRule.HasInitialValue);
            Assert.IsTrue(propertyRule.TrimWhiteSpace);

        }


        [Test]
        public void CheckNonDefaultValuesInitialValue()
        {
            const string expectedInitialValue = "InitialValue";
            const bool expectedUseErrorMessageProvider = true;
            var ruleAttribute = new RequiredStringRuleAttribute
                                                            {
                                                                InitialValue = expectedInitialValue,
                                                                UseErrorMessageProvider = expectedUseErrorMessageProvider,
                                                                IgnoreCase = true
                                                            };
            Assert.AreEqual(expectedInitialValue, ruleAttribute.InitialValue);
            Assert.AreEqual(expectedUseErrorMessageProvider, ruleAttribute.UseErrorMessageProvider);
            Assert.IsNotNull(ruleAttribute.InitialValue);
            Assert.IsTrue(ruleAttribute.IgnoreCase);
            Assert.IsTrue(ruleAttribute.TrimWhiteSpace);

            var propertyRule = (RequiredStringRule)ruleAttribute.CreateRule(new MockPropertyDescriptor<string>("foo"));
            var parameterRule = (RequiredStringRule)ruleAttribute.CreateRule(new MockParameterDescriptor<string>("foo"));
            
            Assert.AreEqual(expectedInitialValue, parameterRule.InitialValue);
            Assert.IsTrue(parameterRule.HasInitialValue);
            Assert.IsTrue(parameterRule.TrimWhiteSpace);
            Assert.IsTrue(parameterRule.IgnoreCase.Value);

            Assert.AreEqual(expectedInitialValue, propertyRule.InitialValue);
            Assert.IsTrue(propertyRule.HasInitialValue);
            Assert.IsTrue(propertyRule.TrimWhiteSpace);
            Assert.IsTrue(propertyRule.IgnoreCase.Value);

        }


        [Test]
        public void CallAttributeTester()
        {
            AttributeTester.CheckDefaultValues<string>(new RequiredStringRuleAttribute(), true, true);
            AttributeTester.CheckNonDefaultValues<string>(new RequiredStringRuleAttribute(), true, true);
        }

    }
}